/// <reference path="../_all.ts" />

module Application.Services {

    export class MemberService implements Application.Interfaces.Services.IMemberService {
        _memberClass: Application.Resources.IMemberClass;
        _membershipClass: Application.Resources.IMemberClass;
        _memberServerClass: Application.Resources.IMemberClass;

        public static $inject = [
            '$http',
            '$resource',
        ];

        constructor(public Http: ng.IHttpService, public Resource: ng.resource.IResourceService) {
            this._memberClass = <Application.Resources.IMemberClass> this.Resource(Application.Services.ServiceUtility.GetServiceURL('members/:id'), null, {
                saveCollection: { method: 'POST', isArray: true }
            });
            this._membershipClass = <Application.Resources.IMemberClass> this.Resource(Application.Services.ServiceUtility.GetServiceURL('members/memberships'), null, null);
            this._memberServerClass = <Application.Resources.IMemberClass> this.Resource(Application.Services.ServiceUtility.GetServiceURL('members/servers/:id'), null, {
                saveCollection: { method: 'POST', isArray: true }
            });
        }

        GetMembers(callback: Function) {
            this._memberClass.query(
                function (data: any, h: any) {
                    callback(data, null);
                },
                function (err: any) {
                    callback(null, err);
                });
        }

        UpdateMemberShips(callback: Function) {
            this._membershipClass.get(
                function (data: any, h: any) {
                    callback(data, null);
                },
                function (err: any) {
                    callback(null, err);
                });
        }

        SaveMemberRights(server_id: number, members: Application.Resources.IMember[], callback: Function) {
            this._memberServerClass.saveCollection({ id: server_id }, members,
                function (data: any, h: any) {
                    callback(data, null);
                },
                function (err: any) {
                    callback(null, err);
                });
        }

        DeleteMember(member: Application.Resources.IMember, callback: Function){
            this._memberClass.delete({ id: member.member_id }, null,
                function (data: any, h: any) {
                    callback(data, null);
                },
                function (err: any) {
                    callback(null, err);
                });
        }
    };
}